package com.henu.engineer.service.teacher

import com.henu.engineer.dao.entity.teachingoutline.CourseMethod
import com.henu.engineer.dao.entity.teachingoutline.CourseTeach
import com.henu.engineer.dao.mapper.teacher.TeacherCourseXmlMapper
import com.henu.engineer.dao.mapper.teachingoutline.*
import com.henu.engineer.service.FileService
import com.henu.engineer.service.LoginService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.configurationprocessor.json.JSONArray
import org.springframework.boot.configurationprocessor.json.JSONObject
import org.springframework.stereotype.Service
import java.io.File
import java.util.*
import kotlin.collections.HashMap
import com.alibaba.fastjson.JSONArray as JSONArray1

/**
 * 课程大纲服务
 * @author noexcept
 * @since 2021/8/12 0:54
 */
@Service
class CourseOutlineService {

    @Autowired
    lateinit var loginService: LoginService

    @Autowired
    lateinit var courseTeachXmlMapper: CourseTeachXmlMapper

    @Autowired
    lateinit var courseGoalXmlMapper: CourseGoalXmlMapper

    @Autowired
    lateinit var courseMethodXmlMapper: CourseMethodXmlMapper

    @Autowired
    lateinit var goalMethodCriterionXmlMapper: GoalMethodCriterionXmlMapper

    @Autowired
    lateinit var goalMethodProportionXmlMapper: GoalMethodProportionXmlMapper

    @Autowired
    lateinit var fileService: FileService

    @Autowired
    lateinit var teacherCourseXmlMapper: TeacherCourseXmlMapper

    /**
     * 返回特定课程id的课程信息
     */
    fun getCourseTeachDetail(courseId: String): CourseTeach? {
        return courseTeachXmlMapper.selectCourseTeachByCourseId(courseId)
    }

    /**
     * 根据登陆时查询当前教师课程表得到的课程id，返回相应的课程信息
     */
    fun getCourseTeachDetails(teacherId: String): List<CourseTeach> {
        var courseIds: List<String> = teacherCourseXmlMapper.selectCourseIdsByTeacherId(teacherId)

        return if (courseIds.isEmpty())
            LinkedList<CourseTeach>()
        else
            courseTeachXmlMapper.selectCourseTeachesByCourseIds(courseIds)
    }

    /**
     * 查看某个课程的所有课程目标
     */
    fun getCourseGoalsByCourseId(courseId: String): JSONObject {
        val rawData = courseGoalXmlMapper.selectCourseGoalsByCourseId(courseId)

        val res = JSONObject()
        if (rawData.isEmpty()) {
            return res
        }
        res.put("courseId", rawData.first().id)
        res.put("courseName", rawData.first().courseName)

        val data = JSONArray()
        rawData.forEach {
            val jsonObject = JSONObject().apply {
                put("id", it.id)
                put("content", it.content)
                put("measures", it.measures)
                put("proportion", it.proportion)
                put("gr", it.graduationRequirement)
                put("indicatorId", it.indicatorId)
            }
            data.put(jsonObject)
        }
        res.put("data", data)
        return res
    }

    /**
     * 返回某个课程的所有考核方式详细信息
     */
    fun getCourseMethods(courseId: String): List<CourseMethod> {
        return courseMethodXmlMapper.selectCourseMethodByCourseId(courseId)
    }

    /**
     * 返回某个课程的课程目标与考核方式的评价标准
     */
    fun goalMethodCriterion(courseId: String): com.alibaba.fastjson.JSONObject {

        val res = com.alibaba.fastjson.JSONObject()
        val rawData = goalMethodCriterionXmlMapper.selectByCourseId(courseId)
        if (rawData.isEmpty()) return res

        val courseTeach = courseTeachXmlMapper.selectCourseTeachByCourseId(courseId)

        res["courseId"] = courseTeach!!.id
        res["courseName"] = courseTeach.name
        val data = JSONArray1()

        rawData.groupBy { it.courseGoalId }.forEach { methods ->
            val singleGoal = com.alibaba.fastjson.JSONObject()
            val list = methods.value
            list.groupBy { it.courseMethodId }.forEach { it ->
                val methodList = it.value

                val jsonArray = JSONArray1()
                methodList.forEach {
                    val jsonObject = com.alibaba.fastjson.JSONObject()
                    jsonObject["courseMethodId"] = it.courseMethodId
                    jsonObject["courseMethodName"] = it.courseMethodName
                    jsonObject["excellent"] = it.excellentCriterion
                    jsonObject["good"] = it.goodCriterion
                    jsonObject["pass"] = it.passCriterion
                    jsonArray.add(jsonObject)
                }
                singleGoal["methods"] = jsonArray
            }
            data.add(singleGoal)
        }
        res["data"] = data
        return res
    }

    /**
     * 返回某个课程的所有目标的所有考核方式占比
     */
    fun getGoalMethodProportion(courseId: String): com.alibaba.fastjson.JSONObject {
        val res = com.alibaba.fastjson.JSONObject()
        val rawData = goalMethodProportionXmlMapper.selectByCourseId(courseId)
        if (rawData.isEmpty()) return res

        val goals = com.alibaba.fastjson.JSONArray()
        rawData.groupBy { it.courseGoalId }.forEach { (i, list) ->
            val goal = com.alibaba.fastjson.JSONObject()

            val methods = com.alibaba.fastjson.JSONArray()
            list.groupBy { it.method }.forEach { it ->
                val methodList = it.value
                methodList.forEach {
                    val method = com.alibaba.fastjson.JSONObject()
                    method["method"] = it.method
                    method["proportion"] = it.proportion
                    methods.add(method)
                }
            }
            goal["methods"] = methods
            goal["goalId"] = i
            goals.add(goal)
        }
        res["data"] = goals
        return res
    }

    /**
     * 生成某课程的课程大纲word文档
     */
    fun generateCourseOutlineDocFile(courseId: String): File {

        // FIXME: 2021/8/12 技术方面已经实现，只是数据等业务方面还没确定
        val map = HashMap<String, Any>().apply {
            put("courseId", courseId)
            put("courseName", "C#")
            put("courseIntroduction", "这是课程简介")
            put("courseGoalBrief", "这是课程目标简介")
            put("coursePosition", "这是地位")
            put("courseGoal1", "这是课程目标一内容")
            put("formulateName", "这是大纲指制定人")
            put("auditorName", "这是大纲审定人")
            put("approverName", "这是大纲批准人")
            put("formulateTime", "这是制定时间")
        }

        return fileService.generateWord("courseOutlineTemplates.ftl", map, courseId)
    }
}